<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @subpackage Tags
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: PlaceObject.php 5 2010-02-14 05:08:39Z indigo.sunrise $
 */

/**
 * Represents the PlaceObject tag.
 *
 * @package    Pwiff
 * @subpackage Tags
 */
class Pwiff_Tag_PlaceObject extends Pwiff_Tag
{
	/**
	 * PlaceObject tag type.
	 * 
	 * @var int
	 */
	protected $pwiffTag = 4;
	
	/**
	 * Character ID.
	 *
	 * @var int
	 */
	protected $characterId;
	
	/**
	 * Depth.
	 *
	 * @var int
	 */
	protected $depth;
	
	/**
	 * Transformation matrix.
	 * 
	 * @var Pwiff_Record_Matrix
	 */
	protected $matrix;
	
	/**
	 * Color transform. Optional.
	 * 
	 * @var Pwiff_Record_CXForm
	 */
	protected $colorTransform;
	
	/**
	 * Sets the character ID.
	 *
	 * @param  int $characterId
	 * @return Pwiff_Tag_PlaceObject
	 */
	public function setCharacterId($characterId)
	{
		$this->characterId = $characterId;
		return $this;
	}
	
	/**
	 * Sets the depth.
	 * 
	 * @param  int $depth
	 * @return Pwiff_Tag_PlaceObject
	 */
	public function setDepth($depth)
	{
		$this->depth = $depth;
		return $this;
	}
	
	/**
	 * Sets the transformation matrix.
	 * 
	 * @param  Pwiff_Record_Matrix $matrix
	 * @return Pwiff_Tag_PlaceObject
	 */
	public function setMatrix(Pwiff_Record_Matrix $matrix)
	{
		$this->matrix = $matrix;
		return $this;
	}
	
	/**
	 * Sets the color transformation.
	 * 
	 * @param  Pwiff_Record_CXForm|null $cxform
	 * @return Pwiff_Tag_PlaceObject
	 */
	public function setColorTransform($cxform)
	{
		$this->colorTransform = $cxform;
		return $this;
	}
	
	/**
	 * Gets the character ID
	 *
	 * @return int
	 */
	public function getCharacterId()
	{
		return $this->characterId;
	}
	
	/**
	 * Gets the depth.
	 * 
	 * @return int
	 */
	public function getDepth()
	{
		return $this->depth;
	}
	
	/**
	 * Gets the transformation matrix.
	 * 
	 * @return Pwiff_Record_Matrix
	 */
	public function getMatrix()
	{
		return $this->matrix;
	}
	
	/**
	 * Gets the color transformation.
	 * 
	 * @return Pwiff_Record_CXForm
	 */
	public function getColorTransform()
	{
		return $this->colorTransform;
	}
	
	/**
	 * Gets the PlaceObject tag's length.
	 * 
	 * @return int
	 */
	public function getTagLength()
	{
		$length  = 4; // characterID + depth
		$length += $this->matrix->getRecordLength();
		if ($this->colorTransform !== null) {
			$length += $this->colorTransform->getRecordLength();
		}
		
		return $length;
	}
	
	/**
	 * Writes the PlaceObject tag to the given output instance.
	 * 
	 * @param  Pwiff_Output $out
	 */
	public function write(Pwiff_Output $out)
	{
		parent::write($out);
		
		$out->writeUI16($this->characterId);
		$out->writeUI16($this->depth);
		
		$this->matrix->write($out);
		if ($this->colorTransform !== null) {
			$this->colorTransform->write($out);
		}
	}
}
